/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.util.List;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Node;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.server.LTServerFactory;
import pt.utl.ist.isr.livetrack.ui.Settings.Server;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class ServersNodeChildFactory extends ChildFactory<String> {

    private static ServersNodeChildFactory instance = new ServersNodeChildFactory();

    public static ServersNodeChildFactory getInstance() {
        return instance;
    }

    private ServersNodeChildFactory() {
    }

    @Override
    protected Node createNodeForKey(String key) {
        for (Server s : Settings.loadSettings().getServers()) {
            if (s.toString().equals(key)) {
                LTServer server = LTServerFactory.getLTServer(s.getHostname(), s.getPort());
                if (server != null) {
                    return new ServerNode(s, server);
                } else {
                    return new DisconnectedServerNode(s);
                }
            }
        }
        return null;
    }

    @Override
    protected boolean createKeys(List<String> toPopulate) {
        for (Server server : Settings.loadSettings().getServers()) {
            toPopulate.add(server.toString());
        }
        return true;
    }

    public void refresh() {
        super.refresh(true);
    }
}
